/*
 * 
 *方法的整理与重构
 *
 *
 */

//定义app参数名称，并加载其它模块
var authApp=angular.module('authapp', ["ngCookies",'ngFileUpload','ui.bootstrap']);

//此为定位页面所用
authApp.value('page','improve_suggest');

/*controller部分*/

//主要操作controller
authApp.controller("opinionEditController",function($scope,$http,$getQueryString,$timeout,$sce){
	$scope.anum=0;
	$scope.bnum=0;
	$scope.cnum=0;
	
	//**********主体方法
	
	$scope.isEdit=true;
	var queryMainApi=apis.queryAuthExpertAdviceObservation;	
	var requestJson={};	
	$scope.queryMain=function(){
		$http({
			method:'get',
			url:queryMainApi,
			params:requestJson,
		}).success(function(response){
			if(response.code=="000000"){
				$scope.queryMainApiCallBack(response);
			}else{
				alert(response.errorMsg);
			}
		}).error(function(){
			alert("网络出现故障，请确认网络是否连接");
		})
	}
	$scope.queryMain();
	
	/*下面为自已编辑部分*/
	$scope.initData=function(){
		$scope.list=new Array();
		for(var i=0;i<5;i++){
			$scope.list[i]={
					num:i+1,
					keywords:"",
					suggestion:"",
			}
		}
	}
	//$scope.initData();
	
	//调用主接口成功之后的处理方法
	$scope.queryMainApiCallBack=function(response){
		if(response.code=="000000"){
			$scope.list=response.data;
			if(response.data.length==0){
				$scope.isEdit=true;
				$scope.initData();
			}else{
				$scope.isEdit=false;
				angular.forEach($scope.list,function(data,index,array){
					var k=index+1;
					data.num=k;
					if(data.detailedAdvice=="")
						data.detailedAdvice="-";
					if(data.keyword=="")
						data.keyword="-";
				})
			}
		}else{
			alert(response.errorMsg);
		}
	}
	/*
	 * 
	 * 其它操作方法
	 * 
	 */

	//表格数据提交
	$scope.uploadAll=function(){
		if(window.confirm("是否确认提交数据？提交后不能修改！")){
			var isAllow=true;
			var keyWords="";
			var suggestions="";
			
			angular.forEach($scope.list,function(data,index,array){
				if(data.keyword)
					keyWords+=data.keyword+"#";
				else
					keyWords+=" #";
				
				if(data.detailedAdvice)
					suggestions+=data.detailedAdvice+"#";
				else
					suggestions+=" #";
			})
			
			if(isAllow){
				var data={'keyWords':keyWords,'suggestions':suggestions};
				$http({
					url:apis.saveAuthExpertAdviceObservation,
					method:"post",
					data:data,
				}).success(function(response){
					if(response.code=="000000"){
						alert("保存数据成功！");
						location.href="improve_suggest.html";
					}else if(response.code=="100000"){
						alert("保存数据失败！");
					}
				})
			}else{
				alert("信息未填充完整");
			}
		}
	}
})
